package controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import model.Blog;
import model.BlogDao;
import model.User;
import model.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2023-12-08
 * Time：15:46
 */
@WebServlet("/userInfo")
public class UserInfoServlet extends HttpServlet {
    ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String blogId = req.getParameter("blogId");
        //获取登陆信息
        HttpSession session = req.getSession(false);
        if (session == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登陆已失效，请重新登陆！");
            return;
        }
        User user = (User) session.getAttribute("user");
        if (user == null) {
            resp.setStatus(403);
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("登陆已失效，请重新登陆！");
            return;
        }
        if (blogId == null) {
            //1.博客列表页发送的ajax

            //更新返回的用户信息
            UserDao userDao = new UserDao();
            User temp = userDao.selectById(user.getUserId());
            //将password置为空保证安全
            user.setPassword("");
            String jsonString = objectMapper.writeValueAsString(temp);
            resp.setContentType("application/json;charset=utf8");
            resp.getWriter().write(jsonString);
        } else {
            //2.博客详情页发送的ajax
            BlogDao blogDao = new BlogDao();
            Blog blog = blogDao.selectOne(Integer.parseInt(blogId));
            if (blog == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("当前blogId有误！");
                return;
            }
            int userId = blog.getUserId();
            UserDao userDao = new UserDao();
            //获取博客的作者
            User author = userDao.selectById(userId);
            if (author == null) {
                resp.setStatus(403);
                resp.setContentType("text/html;charset=utf8");
                resp.getWriter().write("当前博客作者有误 ！");
                return;
            }
            author.setPassword("");
            //判断author和user是否是同一个人
            if (user.getUserId() == author.getUserId()) {
                author.setIsYourBlog(1);
            } else {
                author.setIsYourBlog(0);
            }
            resp.setContentType("application/json;charset=utf8");
            String jsonString = objectMapper.writeValueAsString(author);
            PrintWriter writer = resp.getWriter();
            writer.write(jsonString);
        }
    }
}
